/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.kimstore.gui.sales;

import com.kimstore.main.controller.OrdersTabController;
import com.kimstore.bean.Customer;
import com.kimstore.bean.Item;
import com.kimstore.bean.ItemStatus;
import com.kimstore.bean.Order;
import com.kimstore.bean.Staff;
import com.kimstore.factory.DaoFactory;
import java.awt.Dialog;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author Administrator
 */
public class NewOrderController {

    private NewOrderWindow newOrderWindow;
    private OrdersTabController ordersTabController;
    private Order order;
    private List<Staff> staffs = null;
    private List<Customer> customers = null;

    public NewOrderController(OrdersTabController ordersTabController) {
        this.ordersTabController = ordersTabController;
        this.order = new Order();

        staffs = DaoFactory.getInstance().getStaffDao().findAll();
        customers = DaoFactory.getInstance().getCustomerDao().findAll();
    }

    public void displayWindow() {
        newOrderWindow = new NewOrderWindow(this);
        newOrderWindow.populateCustomers(customers);
        newOrderWindow.populateStaffs(staffs);
        newOrderWindow.showWindow();
    }

    public void fromWindow(Staff staff, Customer customer, String customerType) {
        Date now = new Date();
        for (Item i : order.getItems()) {
            i.getPurchaseDetail().setDateSold(now);
            i.getPurchaseDetail().setCustomer(customer);

            ItemStatus status = new ItemStatus();
            status.setType(ItemStatus.ORDERED);
            status.setDate(now);
            status.setStaff(staff);

            status.setRemark("Order placed by customer (" + ((customer != null) ? customer.getName() + ")." : ""));

            i.addHistory(status);
        }

        order.setCustomer(customer);
        order.setCustomerType(customerType);
        order.setStaff(staff);
        order.setOrderDate(now);

        DaoFactory.getInstance().getOrderDao().saveOrUpdate(order);

        ordersTabController.createNewOrderWindowCallback(order);
    }

    public void createAddItemController() {
        AddItemToOrderController addItemController = new AddItemToOrderController(this);
        addItemController.displayWindow();
    }

    public void createAddItemControllerCallback(Item item, ArrayList<Item> freebies) {
        DefaultTableModel tableModel = (DefaultTableModel) newOrderWindow.getItemsTable().getModel();
       
        order.addItem(item);
        
        tableModel.insertRow(tableModel.getRowCount(), new Object[]{item.getProduct().getName(), item.getSerial(), item.getPurchaseDetail().getPriceSold() + item.getPurchaseDetail().getDiscount(), item.getPurchaseDetail().getPriceSold()});

        for(Item items: freebies) {
            order.addItem(items);
            
            tableModel.insertRow(tableModel.getRowCount(), new Object[]{items.getProduct().getName(), items.getSerial(), items.getPurchaseDetail().getPriceSold() + items.getPurchaseDetail().getDiscount(), items.getPurchaseDetail().getPriceSold()});
        }

              
    }

    public List<Item> getItems() {
        return order.getItems();
    }
    

    void removeItem(String sku) {
        for(Item item: order.getItems()) {
            if(item.getSerial().equals(sku)) {
                order.getItems().remove(item);
                break;
            }
        }
    }
    
    public void validate() throws Error {
        if(order.getItems() == null || order.getItems().size() < 1) {
            throw new Error("<html>No <b>Item</b> has been added</html>");
        }
    }
}
